﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using QUANLYCHAMCONG.BL.BUS;
using QUANLYCHAMCONG.BL.Entities;
using System.Reflection;
using Microsoft.VisualBasic.CompilerServices;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using Hopthoai;

namespace QUANLYCHAMCONG.PL.CMS.BaoCao
{
    public partial class Tongcong : UserControl
    {
        public Tongcong()
        {
            InitializeComponent();
        }
        private void SetLink()
        {
            this.links1.SetValues("Báo cáo", "Tình hình lao động trong ngày");
        }
        System.Data.DataTable ds = new System.Data.DataTable();
        private void Tongcong_Load(object sender, EventArgs e)
        {
            SetLink();
            taocautruc();
        }
        public static Microsoft.Office.Interop.Excel.Application ExApp;
        public static Microsoft.Office.Interop.Excel.Workbook TasWBook;
        public static Microsoft.Office.Interop.Excel.Worksheet TasWSheet = new Microsoft.Office.Interop.Excel.Worksheet();
         System.Data.DataTable Create()
        {
            //tao cau truc 
            //duyet tu bang nhan duoc 
             ds.Rows.Clear();
          
            Thongke bc = new Thongke();
            bc.NGAYBD = dtpngaybd.Value;
            System.Data.DataTable dt = new System.Data.DataTable();
            dt=BAOCAOBUS.ThongkeTinhhinh(bc);
            if (dt.Rows.Count != 0)
            {
            }
            else
            {
              dt= BAOCAOBUS.ThongKeNeuKhongcodulieu(bc);
            }
            int i =1;
            if (dt.Columns.Count==2)
            {
                foreach(DataRow dr in dt.Rows)
                {
                    DataRow row =ds.NewRow();
                    row["STT"]=i.ToString();
                    row["TENPB"]=dr["TENPB"].ToString();
                    row["TONGSONV"]=  dr["TONGSONV"].ToString();
                    row["SONVCOMAT"]="0";
                    row["SONVVANG"]= dr["TONGSONV"].ToString();
                    i++;
                    ds.Rows.Add(row);
                }
            } 
            else
            {
                foreach(DataRow dr in dt.Rows)
                {
                   
                     DataRow row =ds.NewRow();
                    row["STT"]=i.ToString();
                    row["TENPB"]=dr["TENPB"].ToString();
                    row["TONGSONV"]=  dr["TONGSONV"].ToString();
                    row["SONVCOMAT"]=dr["SOLUONGCOMAT"].ToString();
                    row["SONVVANG"]= (int.Parse(dr["TONGSONV"].ToString())-int.Parse(dr["SOLUONGCOMAT"].ToString())).ToString();
                    i++;
                     ds.Rows.Add(row);
                }
            }
            return ds;
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {

            try
            {
                dgvthongke.DataSource = Create();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
         public System.Drawing.Point DoPhanGiaiManhinh()
        {
            System.Drawing.Point p = new System.Drawing.Point();
            Size s = SystemInformation.WorkingArea.Size;
            p.X = Size.Width / 2;
            p.Y = Size.Height / 2;
            return p;
        }
         void taocautruc()
         {
             DataColumn STT = new DataColumn("STT", typeof(string));
             DataColumn TENPB = new DataColumn("TENPB", typeof(string));
             DataColumn TONGSONV = new DataColumn("TONGSONV", typeof(string));
             DataColumn SONVCOMAT = new DataColumn("SONVCOMAT", typeof(string));
             DataColumn SONVVANG = new DataColumn("SONVVANG", typeof(string));
             ds.Columns.AddRange(new DataColumn[] { STT, TENPB, TONGSONV, SONVCOMAT, SONVVANG });
         }
        private void btninchamcong_Click(object sender, EventArgs e)
        {
            TasStartExcel("THONG KE TINH HINH LAO DONG");
            int count = ds.Rows.Count;
            // doc do phan giai man hinh
            frmwaiting f = new frmwaiting();
            f.Show();
            f.lbgoi.Text = "Đang duyệt...";
            f.lbphantram.Visible = true;
            f.tocdo.Visible = true;
            f.tocdo.Maximum = count;
            f.Location = DoPhanGiaiManhinh();
            f.Activate();
            int i = 0;
         
            System.Windows.Forms.Application.DoEvents();
            if (ds.Rows.Count != 0)
            {
                try
                {
                    Microsoft.Office.Interop.Excel.Worksheet tasWSheet = TasWSheet;
                    HederThongKe();
                    foreach (DataRow rows in ds.Rows)
                    {
                        f.tocdo.Value = i;
                        f.lbphantram.Text = Conversions.ToString((double)(Math.Round((double)(((double)i) / ((double)count)), 2) * 100.0)) + "%";
                        System.Windows.Forms.Application.DoEvents();
                        f.Activate();
                        tasWSheet.get_Range("A" + Conversions.ToString(i + 6) + ":E" + Conversions.ToString((int)(i + 6)), Missing.Value).Borders[XlBordersIndex.xlEdgeLeft].LineStyle = 1;
                        tasWSheet.get_Range("A" + Conversions.ToString(i + 6) + ":E" + Conversions.ToString((int)(i + 6)), Missing.Value).Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                        tasWSheet.get_Range("A" + Conversions.ToString(i + 6) + ":E" + Conversions.ToString((int)(i + 6)), Missing.Value).Borders[XlBordersIndex.xlEdgeBottom].LineStyle = 3;
                        tasWSheet.get_Range("A" + Conversions.ToString(i + 6), Missing.Value).set_Value(Missing.Value, "'" + (i + 1).ToString());
                        tasWSheet.get_Range("A" + Conversions.ToString(i + 6), Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
                        tasWSheet.get_Range("A" + Conversions.ToString(i + 6), Missing.Value).Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                        //phong ban
                        tasWSheet.get_Range("B" + Conversions.ToString(i + 6), Missing.Value).set_Value(Missing.Value, rows["TENPB"].ToString());
                        tasWSheet.get_Range("B" + Conversions.ToString(i + 6), Missing.Value).Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                        //tong so nv 
                        tasWSheet.get_Range("C" + Conversions.ToString(i + 6), Missing.Value).set_Value(Missing.Value, rows["TONGSONV"].ToString());
                        tasWSheet.get_Range("C" + Conversions.ToString(i + 6), Missing.Value).Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                        //comat 
                        tasWSheet.get_Range("D" + Conversions.ToString(i + 6), Missing.Value).set_Value(Missing.Value, rows["SONVCOMAT"].ToString());
                        tasWSheet.get_Range("D" + Conversions.ToString(i + 6), Missing.Value).Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                        //vang mat 
                        tasWSheet.get_Range("E" + Conversions.ToString(i + 6), Missing.Value).set_Value(Missing.Value, rows["SONVVANG"].ToString());
                        tasWSheet.get_Range("E" + Conversions.ToString(i + 6), Missing.Value).Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                        i++;
                    }
                    //song 
                    FooterChiTiet(i + 6);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Lỗi :" + ex.Message.ToString());
                    f.Close();
                }
                finally
                {
                    f.Close();
                }
 

            }
            else
            {
                frmHoidap hoi = new frmHoidap("Dữ liệu trống!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                hoi.ShowDialog();
                f.Close();
            }
        }
        public void FooterChiTiet(int dong)
        {
            TasWSheet.get_Range("A" + Conversions.ToString(dong) + ":E" + Conversions.ToString(dong), Missing.Value).Interior.Color = 0xc0c0c0;
            TasWSheet.get_Range("A6:E" + Conversions.ToString(dong), Missing.Value).Borders.LineStyle = 1;
            if (dong > 7)
            {
                TasWSheet.get_Range("A6:E" + Conversions.ToString((int)(dong - 1)), Missing.Value).Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = 3;
            }
        }
        public void TasStartExcel([Optional, DefaultParameterValue("Sheet1")] string SheetName)
        {
            try
            {
                ExApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                TasWBook = ExApp.Workbooks.Add(Missing.Value);
                TasWSheet = (Microsoft.Office.Interop.Excel.Worksheet)ExApp.Worksheets["Sheet1"];
                TasWSheet.Name = SheetName;
                ExApp.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        void HederThongKe()
        {
            Microsoft.Office.Interop.Excel.Worksheet tasWSheet = TasWSheet;
            //header 
            tasWSheet.get_Range("A" + Conversions.ToString((long)(2)) + ":E" + Conversions.ToString((long)(2)), Missing.Value).Merge(Missing.Value);
            tasWSheet.get_Range("A" + Conversions.ToString((long)(2)) + ":E" + Conversions.ToString((long)(2)), Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("A" + Conversions.ToString((long)(2)), Missing.Value).set_Value(Missing.Value, "BẢNG THỐNG KÊ TÌNH HÌNH LAO ĐỘNG");
            tasWSheet.get_Range("A" + Conversions.ToString((long)(2)), Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("A" + Conversions.ToString((long)(2)), Missing.Value).Font.Size = 16;
            tasWSheet.get_Range("A" + Conversions.ToString((long)(2)), Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            //dong tiep theo
            //su ly data
            string dengnay = dtpngaybd.Value.Day.ToString() + "-" + dtpngaybd.Value.Month.ToString() + "-" + dtpngaybd.Value.Year.ToString();
            tasWSheet.get_Range("A" + Conversions.ToString((long)(3)) + ":E" + Conversions.ToString((long)(3)), Missing.Value).Merge(Missing.Value);
            tasWSheet.get_Range("A" + Conversions.ToString((long)(3)) + ":E" + Conversions.ToString((long)(3)), Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("A" + Conversions.ToString((long)(3)), Missing.Value).set_Value(Missing.Value, "NGÀY : " + dengnay);
            tasWSheet.get_Range("A" + Conversions.ToString((long)(3)), Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("A" + Conversions.ToString((long)(3)), Missing.Value).Font.Size = 11;
            tasWSheet.get_Range("A" + Conversions.ToString((long)(3)), Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            tasWSheet.get_Range("A" + Conversions.ToString(5), Missing.Value).set_Value(Missing.Value, "STT");
            tasWSheet.get_Range("B" + Conversions.ToString(5), Missing.Value).set_Value(Missing.Value, "Tên phòng ban");
            tasWSheet.get_Range("C" + Conversions.ToString(5), Missing.Value).set_Value(Missing.Value, "Tổng số");
            tasWSheet.get_Range("D" + Conversions.ToString(5), Missing.Value).set_Value(Missing.Value, "Có mặt");
            tasWSheet.get_Range("E" + Conversions.ToString(5), Missing.Value).set_Value(Missing.Value, "Vắng mặt");
            tasWSheet.get_Range("A5", Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("A5", Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("A5", Missing.Value).Font.Size = 12;
            NewLateBinding.LateSetComplex(tasWSheet.Columns["A", Missing.Value], null, "ColumnWidth", new object[] { 6 }, null, null, false, true);
            tasWSheet.get_Range("B5", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            tasWSheet.get_Range("B5", Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("B5", Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("B5", Missing.Value).Font.Size = 12;
            tasWSheet.get_Range("B5", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            NewLateBinding.LateSetComplex(tasWSheet.Columns["B", Missing.Value], null, "ColumnWidth", new object[] { 20 }, null, null, false, true);
            tasWSheet.get_Range("C5", Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("C5", Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("C5", Missing.Value).Font.Size = 12;
            tasWSheet.get_Range("C5", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            NewLateBinding.LateSetComplex(tasWSheet.Columns["C", Missing.Value], null, "ColumnWidth", new object[] { 20 }, null, null, false, true);
            tasWSheet.get_Range("D5", Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("D5", Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("D5", Missing.Value).Font.Size = 12;
            tasWSheet.get_Range("D5", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            NewLateBinding.LateSetComplex(tasWSheet.Columns["D", Missing.Value], null, "ColumnWidth", new object[] { 20 }, null, null, false, true);
            tasWSheet.get_Range("E5", Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("E5", Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("E5", Missing.Value).Font.Size = 12;
            tasWSheet.get_Range("E5", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            NewLateBinding.LateSetComplex(tasWSheet.Columns["E", Missing.Value], null, "ColumnWidth", new object[] { 10 }, null, null, false, true);
           
        }
        public delegate void _Close();
        public event _Close Close;
        private void btnthoat_Click(object sender, EventArgs e)
        {
            if (this.Close != null)
                this.Close();
        }

    }
}
